<template>
  <div>
    <van-cell-group>
      <div v-if='needRegister'>
        <div>
          <swiper autoplay>
            <swiper-item :key="url" v-for="url in swiperPictures">
              <image :src="url" alt="docker" mode="aspectFit" lazy-load></image>
            </swiper-item>
          </swiper>
          <span>帮助您方便快捷的管理docker应用容器引擎</span>
          <van-button type="primary" open-type="getUserInfo" @getuserinfo="toRegister" size="large">授权</van-button>
        </div>
      </div>
      <div v-else>
        <div v-if="needUser">
          <navigator open-type="exit" target="miniProgram">退出</navigator>
        </div>
      </div>
    </van-cell-group>
    <van-toast id="van-toast"></van-toast>
    <van-dialog id="van-dialog"></van-dialog>
  </div>
</template>

<script>
  import indexAPI from '@/api/indexAPI'
  import Toast from '@/../static/vant-weapp/toast/toast'
  import Dialog from '@/../static/vant-weapp/dialog/dialog'

  export default {
    name: 'index',
    data () {
      return {
        userName: '',
        needUser: false,
        needRegister: false,
        swiperPictures: [
          '/static/picture/docker.jpg',
          '/static/picture/docker.jpg',
          '/static/picture/docker.jpg'
        ]
      }
    },
    onShow () {
      wx.startPullDownRefresh()
    },
    onPullDownRefresh () {
      var that = this
      that.login()
    },
    methods: {
      login () {
        var that = this
        wx.login({
          success (result) {
            var param = {
              code: result.code
            }
            indexAPI.login(param).then(result => {
              wx.setStorageSync('sessionKey', result.data.sessionKey)
              if (result.data.token === undefined) {
                that.needRegister = true
                // Dialog.confirm({
                //   message: '帮助您方便快捷的管理docker应用容器引擎',
                //   showConfirmButton: true,
                //   showCancelButton: true,
                //   confirmButtonText: '授权',
                //   cancelButtonText: '取消',
                //   confirmButtonColor: '#00CD00',
                //   cancelButtonColor: '#000000'
                // }).then(() => {
                // }).catch(() => {
                // })
              } else {
                that.needRegister = false
                wx.setStorageSync('token', result.data.token)
                wx.setStorageSync('wxUser', result.data.wxUser)
                if (result.data.user === undefined) {
                  that.needUser = true
                  Dialog.confirm({
                    message: '绑定电子邮件',
                    showConfirmButton: true,
                    showCancelButton: true,
                    confirmButtonText: '授权',
                    cancelButtonText: '取消',
                    confirmButtonColor: '#00CD00',
                    cancelButtonColor: '#000000'
                  }).then(() => {
                    wx.redirectTo({
                      url: '/pages/user/to'
                    })
                  }).catch(() => {
                  })
                } else {
                  that.needUser = false
                  wx.setStorageSync('user', result.data.user)
                  that.toShow()
                }
              }
              wx.stopPullDownRefresh()
            })
          }
        })
      },
      toRegister (e) {
        var errMsg = e.mp.detail.errMsg
        if (errMsg === 'getUserInfo:ok') {
          wx.redirectTo({
            url: '/pages/user/register'
          })
        } else {
          Toast.fail({
            message: '未授权',
            forbidClick: true
          })
        }
      },
      toShow () {
        wx.switchTab({
          url: '/pages/tabbar/docker'
        })
      }
    }
  }
</script>

<style scoped>
</style>
